
// Clase AlertWindow

var ALERT_TITLE_HEIGHT = 15;
var ALERT_WINDOW_HEIGHT = 100;
var ALERT_WINDOW_WIDTH = 300;
var ALERT_BUTTONBAR_HEIGHT = 15;

function AlertWindow(id)
{
    this.superClase = FloatingWindow;
    this.superClase(id);
    
    this.title = "";
    this.message = "";    
    this.className = "";
    this.buttonText = "Aceptar";
    this.cancelText = "Cancelar";
    
    this.closeButton = true;
    this.acceptButton = true;
    this.cancelButton = true;
    this.onAccept = "";
    
    this.written = false;
        
    this.win;
        
    this.baseShow = FloatingWindow_Show;
    this.show = AlertWindow_Show;
    this.create = AlertWindow_Create;
    this.close = AlertWindow_Close;
    this.setMessage = AlertWindow_SetMessage;
    this.getMessage = AlertWindow_GetMessage;
}


// ----------------------------------------------------------------------------------------------------------------
// Funciones Publicas
// ----------------------------------------------------------------------------------------------------------------

function AlertWindow_Show(modal)
{
    if(!this.written)
        this.create();

    this.baseShow(modal);

    this.written = true;
}

function AlertWindow_Close()
{
    document.body.removeChild(this.win);
    
    if(this.modal)
        Common.activatePage();    
        
    eval(this.onClose);
    
    this.written = false;
}

function AlertWindow_SetMessage(value)
{
    var cellMsg = document.getElementById(this.id + "_msg");
    if(cellMsg)
        cellMsg.innerHTML = value;
}

function AlertWindow_GetMessage(id)
{
    var cellMsg = document.getElementById(this.id + "_msg");
    if(cellMsg)
        return cellMsg.innerHTML;
}


// ----------------------------------------------------------------------------------------------------------------
// Funciones Privadas
// ----------------------------------------------------------------------------------------------------------------

function AlertWindow_Create()
{
    this.win = document.createElement("DIV");
	
    this.win.id = this.id;
    this.win.name = this.id;
    this.win.className = this.className;
  
    Common.setStyleAttribute(this.win, "position", "absolute");
    Common.setStyleAttribute(this.win, "width", ALERT_WINDOW_WIDTH + "px");
  //Common.setStyleAttribute(this.win, "height", ALERT_WINDOW_HEIGHT + "px");       
    Common.setStyleAttribute(this.win, "display", "none");
    Common.setStyleAttribute(this.win, "cursor", "move");
 
    Common.setEventHandler(this.win, "onmousedown", "FloatingWindow_DragStart('" + this.id +"', event)")
    

    // Tabla Principal
    var mainTable = document.createElement("TABLE");   
    mainTable.cellPadding = "0";
    mainTable.cellSpacing = "0";
    mainTable.setAttribute("width", "100%");        
    var row = mainTable.insertRow(0);
    var cell = row.insertCell(row.cells.length);
    
    this.win.appendChild(mainTable);

    // Tabla titulo
    var titleTable = document.createElement("TABLE");    
    titleTable.cellPadding = "0";
    titleTable.cellSpacing = "2";
    titleTable.setAttribute("width", "100%");
    titleTable.setAttribute("height", ALERT_TITLE_HEIGHT);
    titleTable.setAttribute("border", "0");
    cell.appendChild(titleTable);
    
    // Titulo
    row = titleTable.insertRow(0);
    cell  = row.insertCell(row.cells.length);
    cell.className = "titleBar";
    cell.setAttribute("align", "left");
    cell.innerHTML = this.title;
    
    // Boton cerrar
    if(this.closeButton) {
        cell  = row.insertCell(row.cells.length);
        cell.setAttribute("align", "right");
        var divButton = document.createElement("div");
        divButton.className = "closeButton";
        divButton.setAttribute("classRollOver", "closeButtonOn");    
        Common.setEventHandler(divButton, "onmouseover", "Common.on(this)");
        Common.setEventHandler(divButton, "onmouseout", "Common.off(this)");            
        Common.setEventHandler(divButton, "onclick", "AlertWindow_Get('" + this.id +"').close()")
        cell.appendChild(divButton);
    }
    
    // Content
    row = mainTable.insertRow(1);
    cell = row.insertCell(row.cells.length);
    cell.id = this.id + "_msg";
    cell.setAttribute("align", "center");
    cell.className = "content";    
    Common.setStyleAttribute(cell, "height", (ALERT_WINDOW_HEIGHT - ALERT_TITLE_HEIGHT - ALERT_BUTTONBAR_HEIGHT - 10) + "px");

    // Mensaje
    cell.innerHTML = this.message;

    // Barra de botones
    row = mainTable.insertRow(2);
    cell = row.insertCell(row.cells.length);
    cell.setAttribute("align", "center");
    Common.setStyleAttribute(cell, "height", ALERT_BUTTONBAR_HEIGHT + "px");
    
    var buttonsBarTable = document.createElement("TABLE");    
    cell.appendChild(buttonsBarTable);

    row = buttonsBarTable.insertRow(0);
 
    // Boton Aceptar
    if(this.acceptButton) {    
        cell = row.insertCell(row.cells.length);
        divButton = document.createElement("div");
        divButton.className = "button";    
        divButton.setAttribute("classRollOver", "buttonOn");    
        divButton.innerHTML = this.acceptText;
        Common.setEventHandler(divButton, "onmouseover", "Common.on(this)");
        Common.setEventHandler(divButton, "onmouseout", "Common.off(this)");    
        
        if(this.onAccept)
            Common.setEventHandler(divButton, "onclick", this.onAccept + "; AlertWindow_Get('" + this.id +"').close()")    
        else
            Common.setEventHandler(divButton, "onclick", "AlertWindow_Get('" + this.id +"').close()")    

        cell.appendChild(divButton);
    }
    
    // Boton Cancelar
    if(this.cancelButton) {        
        cell = row.insertCell(row.cells.length);
        divButton = document.createElement("div");
        divButton.className = "button";    
        divButton.setAttribute("classRollOver", "buttonOn");    
        divButton.innerHTML = this.cancelText;
        Common.setEventHandler(divButton, "onmouseover", "Common.on(this)");
        Common.setEventHandler(divButton, "onmouseout", "Common.off(this)");    
        Common.setEventHandler(divButton, "onclick", "AlertWindow_Get('" + this.id +"').close()")    
        cell.appendChild(divButton);
    }
    
	document.body.appendChild(this.win);    
	
    this.written = true; 
}


// ----------------------------------------------------------------------------------------------------------------
// Funciones Externas
// ----------------------------------------------------------------------------------------------------------------

function AlertWindow_Get(id)
{
    return FloatingWindow_Get(id);
}

